package models

import "gorm.io/gorm"

// Shop 三方平台
type Shop struct {
	ShopId int `gorm:"primary_key" json:"shop_id"` // 店铺 ID

	ShopName     int    `json:"shop_name"`     // 店铺名称
	UserId       int    `json:"user_id"`       // 用户ID
	Platform     int    `json:"platform"`      // 1是抖音
	AccessToken  string `json:"access_token"`  // token
	RefreshToken string `json:"refresh_token"` // 刷新 token
	ExpiresIn    int    `json:"expires_in"`    // 过期时间
	ShopNo       string `json:"shop_no"`       // 三方唯一ID
}

// TableName 表名称
func (*Shop) TableName() string {
	return "shop"
}

// GetShop 查询多条数据
func GetShop(fields []string, maps map[string]interface{}) ([]*Shop, error) {
	var address []*Shop
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetShopPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Shop, error) {
	var Shop []*Shop
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Shop).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Shop, nil
}

// GetShopInfo 查询多条数据
func GetShopInfo(fields []string, maps map[string]interface{}) (*Shop, error) {
	var Shop Shop
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Shop).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Shop, nil
}

// AddShop 插入数据
func AddShopDB(data map[string]interface{}, db *gorm.DB) (int, error) {
	address := Shop{
		ShopName:     data["shop_name"].(int),
		UserId:       data["user_id"].(int),
		Platform:     data["platform"].(int),
		AccessToken:  data["access_token"].(string),
		RefreshToken: data["refresh_token"].(string),
		ExpiresIn:    data["expires_in"].(int),
		ShopNo:       data["shop_no"].(string),
	}
	result := db.Select("shop_name", "user_id", "platform", "access_token", "refresh_token", "expires_in", "shop_no").Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.ShopId, nil
}

// EditShop modify a single Shop
func EditShop(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Shop{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

// EditShop modify a single Shop
func EditShopDB(where map[string]interface{}, data interface{}, db *gorm.DB) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Shop{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetShopTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Shop{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanShop(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Shop{}).Error; err != nil {
		return err
	}

	return nil
}
